/***************************************************************************
 *  Copyright (C) 2009, 2010 by Axel Jaeger <axeljaeger@googlemail.com>    *
 *                                                                         *
 *  This file is part of Glowworm.                                         *
 *                                                                         *
 *  Glowworm is free software: you can redistribute it and/or modify       *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation, version 3 of the License.                *
 *                                                                         *
 *  Glowworm is distributed in the hope that it will be useful,            *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with Glowworm.  If not, see <http://www.gnu.org/licenses/>.      *
 ***************************************************************************/

#ifndef HttpServer_H
#define HttpServer_H

#include <QObject>
#include <QPointer>
#include <QMap>

class QTcpServer;

class AbstractServlet;
class FileServlet;
class LogModel;

class HttpServer : public QObject
{
Q_OBJECT
Q_PROPERTY (bool enabled READ enabled WRITE setEnabled)
Q_PROPERTY (int port READ port WRITE setPort)
Q_PROPERTY (QString wwwRoot READ wwwRoot WRITE setWwwRoot)

public:
    HttpServer(QObject* parent = 0);
    ~HttpServer();

    int port() const;
    bool enabled() const;

    QString wwwRoot() const;

    void addServlet(const QString & prefix, AbstractServlet* servlet);

    LogModel* logModel() const;

public slots:
    void setEnabled(bool enable);
    void setPort(int port);
    void setWwwRoot(const QString & dir);

signals:
    void selectedColor(unsigned char red, unsigned char green, unsigned char blue) const;

private slots:
    void handleIncomingConnection();
    void handleRead();
    void handleDisconnect();

private:
    QTcpServer* m_tcpServer;
    static QString statusMessage(int statusCode, const QString& contentType = QString("text/html"));
    int m_port;
    QString m_wwwRoot;

    QMap<QString, AbstractServlet*> m_servlets;
    QPointer<FileServlet> m_fileServlet;
    QPointer<LogModel> m_logModel;
};

#endif
